//
//  MainNewsViewController.m
//  LoveCar
//
//  Created by Xue Senhong on 15/5/21.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "MainNewsViewController.h"
#import "MenuHrizontal.h"
#import "AutoScrollView.h"
#import "PushViewControllerDelegate.h"
#import "PresentViewDelegate.h"
#import "CollectionCell.h"
#import "VideoCollentionCell.h"
#import "IndexCollentionCell.h"
#import "NewsDetViewController.h"
#import <AFNetworkActivityIndicatorManager.h>


@interface MainNewsViewController () <MenuHrizontalDelegate, UICollectionViewDataSource, UICollectionViewDelegate, PushViewControllerDelegate>

@property (nonatomic, retain) NSMutableArray *channelArray;
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) MenuHrizontal *channel;

@end

@implementation MainNewsViewController

- (void)dealloc
{
    [_channel release];
    [_channelArray release];
    [_aIndex release];
    [_collectionView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.channelArray = [NSMutableArray array];
        self.aIndex = [NSString string];
    }
    return self;
}


- (void)loadView
{
    [super loadView];
    
    self.navigationController.navigationBar.translucent = NO;
    
    self.tabBarController.tabBar.translucent = NO;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    self.channelArray = [NSMutableArray arrayWithArray:@[@{TITLEKEY:@"要闻",
                                                           TITLEWIDTH:[NSNumber numberWithFloat:50]
                                                           },
                                                         @{TITLEKEY:@"视频",
                                                           TITLEWIDTH:[NSNumber numberWithFloat:50]
                                                           },
                                                         @{TITLEKEY:@"新车",
                                                           TITLEWIDTH:[NSNumber numberWithFloat:50]
                                                           },
                                                         @{TITLEKEY:@"评测",
                                                           TITLEWIDTH:[NSNumber numberWithFloat:50]
                                                           },
                                                         @{TITLEKEY:@"导购",
                                                           TITLEWIDTH:[NSNumber numberWithFloat:50]
                                                           }]];

    // 频道选择
    self.channel = [[MenuHrizontal alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 40) ButtonItems:self.channelArray];
    _channel.delegate = self;
    
    // 默认选中状态
    
    [self.channel clickButtonAtIndex:0];
    self.navigationItem.titleView = _channel;
    [_channel release];
    
    // 频道按钮
//    UIBarButtonItem *channel = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"channel.png"] style:UIBarButtonItemStyleDone target:self action:@selector(channelAction:)];
    
    // 横向滚动视图(collectionView)
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setItemSize:self.view.bounds.size];
    
    // 视图滚动方向(horizontal水平方向)
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    // 视图之间的最小间隔
    [flowLayout setMinimumLineSpacing:0];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    // 边缘弹动效果
    [self.collectionView setBounces:NO];
    // 整页滑动
    [self.collectionView setPagingEnabled:YES];
    [self.collectionView setDelegate:self];
    [self.collectionView setDataSource:self];
    [self.collectionView setScrollsToTop:NO];
    [self.view addSubview:self.collectionView];
    // collectionViewCell是一个tableView
    // 文章类型页
    [self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"article"];
    // 视频类型页
    [self.collectionView registerClass:[VideoCollentionCell class] forCellWithReuseIdentifier:@"video"];
    // 首页
    [self.collectionView registerClass:[IndexCollentionCell class] forCellWithReuseIdentifier:@"index"];
    
    [flowLayout release];
    [_collectionView release];
}

#pragma - collectionView -

// collectionViewCell的协议方法
// collectionView滑动 channel跟随滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self.channel clickButtonAtIndex:[[NSString stringWithFormat:@"%f", scrollView.contentOffset.x / self.view.frame.size.width] integerValue]];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        // 首页类型页
        IndexCollentionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"index" forIndexPath:indexPath];
        [cell setDelegate:self];
        return cell;
    } else if (indexPath.item == 1 || indexPath.item == 11) {
        // 视频类型页
        VideoCollentionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"video" forIndexPath:indexPath];
        cell.index = [NSString stringWithFormat:@"%ld", (long)indexPath.item];
        [cell reloadDataForCell];
        [cell setDelegate:self];
        return cell;
    } else {
        // 文章类型页
        CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"article" forIndexPath:indexPath];
        cell.index = [NSString stringWithFormat:@"%ld", (long)indexPath.item];
        [cell reloadDataForCell];
        [cell setDelegate:self];
        return cell;
    }
}

// 实现delegate方法
- (void)pushViewControllerWithString:(NSString *)string
{
    NewsDetViewController *detailVC = [[NewsDetViewController alloc] init];
    detailVC.id = string;
    detailVC.index = self.aIndex;
    [detailVC setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}

// collectionViewCell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [self.channelArray count];
}

#pragma mark - button 方法 -

// 频道滚动条方法
- (void)didMenuHrizontalClickedButtonAtIndex:(NSInteger)aIndex
{
    self.aIndex = [NSString stringWithFormat:@"%ld", (long)aIndex];
    [self.collectionView setContentOffset:CGPointMake(self.collectionView.frame.size.width * aIndex, 0) animated:YES];
}



@end
